import { login, logout, getInfo } from '@/api/authority_app/base'
import { getToken, setToken, removeToken } from '@/utils/auth'

const user = {
  state: {
    token: getToken(),
    name: '',
    deptName: '',
    avatar: '',
    admin: false,
    // 当前租户编号
    tenantCode: '',
    // 当前登录用户机构编号
    deptCode: '',
    // 是否多租户
    multipleCorporate: false,
    mutiDept: false,
    canChangeDept: false,
    permissions: []
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_DEPT_NAME: (state, deptName) => {
      state.deptName = deptName
    },
    SET_ADMIN: (state, admin) => {
      state.admin = admin
    },
    SET_TENANT_CODE: (state, code) => {
      state.tenantCode = code
    },
    SET_MULTIPLE_CORPORATE: (state, multipleCorporate) => {
      state.multipleCorporate = multipleCorporate
    },
    SET_CAN_CHANGE_DEPT: (state, canChangeDept) => {
      state.canChangeDept = canChangeDept
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    },
    SET_DEPT_CODE: (state, deptCode) => {
      state.deptCode = deptCode
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const tenantCode = (userInfo.tenantCode ? userInfo.tenantCode : '')
      const userName = userInfo.userName.trim()
      const passwd = userInfo.passwd
      const verifyCode = userInfo.verifyCode
      const key = userInfo.key
      return new Promise((resolve, reject) => {
        login(tenantCode,userName, passwd, verifyCode,key).then(res => {
          let tokenFlag = 'logined'
          setToken(tokenFlag)
          commit('SET_TOKEN', tokenFlag)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo().then(res => {
          if(res && res.result) {

            const user = res.result
            commit('SET_NAME', user.userName)
            commit('SET_DEPT_NAME', user.deptName)
            commit('SET_ADMIN', user.admin)
            commit('SET_CAN_CHANGE_DEPT', user.mutiDept)
            commit('SET_MULTIPLE_CORPORATE', user.multipleCorporate)
            if(user.hasOwnProperty('tenantCode')) {
              commit('SET_TENANT_CODE', user.tenantCode)
            }
            if(user.hasOwnProperty('deptCode')) {
              commit('SET_DEPT_CODE', user.deptCode)
            }
            if(user.permissions) {
              commit('SET_PERMISSIONS', user.permissions)
            }
          }
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_NAME', '')
          commit('SET_DEPT_NAME', '')
          commit('SET_ADMIN', false)
          commit('SET_TENANT_CODE', '')
          commit('SET_DEPT_CODE', '')
          commit('SET_HAS_FEW_DEPT', false)
          commit('SET_PERMISSIONS', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
